﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LeadTeam.Exam.AdminTool
{
    public partial class OrganizationsManagePanel : ManagePanelBase
    {
        private IOrganizationsView orgView;
        private IMainFormView iMainFormView;
        private OrganizationsManagePanelController _controller;
        public OrganizationsManagePanelController Controllor
        {
            get { return _controller; }
            set
            {
                this._controller = value;
            }
        }

        public OrganizationsManagePanel(string managePanelTitle, IOrganizationsView orgView, IMainFormView iMainFormView)
        {
            InitializeComponent();
            this.lblManagePanelTitle.Text = managePanelTitle;
            this.orgView = orgView;
            this.iMainFormView = iMainFormView;
        }

        /// <summary>
        /// add a new node
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label1_Click(object sender, EventArgs e)
        {
            int id = orgView.GetSelectOrganizationId();
            string name = orgView.GetSelectOrganizationName();

            AddOrganizationsController addOrgController = new AddOrganizationsController(new AddOrganizations(orgView,true),_controller.MainFormHandle);
            if (id != 0 && name != string.Empty)
            {
                addOrgController.view.SetOrganizations(id, name);
            }
            addOrgController.view.ShowDialog();
        }

        /// <summary>
        /// delete a new node
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label2_Click(object sender, EventArgs e)
        {
            int id = orgView.GetSelectOrganizationId();
            if (id == 0)
            {
                MessageBox.Show("未选中组织","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            if (id == 6)
            {
                MessageBox.Show("根节点不能删除");
                return;
            }

            if (DialogResult.Yes == MessageBox.Show("删除此节点\""+orgView.GetSelectOrganizationName()+"\"将会删除所有子节点确认删除吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {

               
                this._controller.model.O_ID = id;
                this._controller.DeleteEntity();
                
                orgView.Controllor.UpdateModel();

                
            }
        }

        /// <summary>
        /// Make it as father
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label3_Click(object sender, EventArgs e)
        {
            if (!orgView.HaveParentParent())
            {
                MessageBox.Show("不能提为父节点", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
              
            }
            else
            {
                int idParentParent = orgView.GetSelectOrganizationParentParentId();
                int id= orgView.GetSelectOrganizationId();
                if (id == 0)
                {
                    MessageBox.Show("未选中组织", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                _controller.model.O_ID = id;
                _controller.model.O_PARENT_ID = idParentParent;
                _controller.UpdateEntityAsParent();

                _controller.SetRequestState();
                orgView.Controllor.UpdateModel();
                _controller.SetDoneState();
            }
        }

        //make as children node
        private void label4_Click(object sender, EventArgs e)
        {
            int idParentParent = orgView.GetSelectOrganizationParentId();
            int id = orgView.GetSelectOrganizationId();
            if (id == 0)
            {
                MessageBox.Show("未选中组织", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _controller.model.O_ID = id;
            _controller.model.O_PARENT_ID = idParentParent;
            _controller.UpdateEntityAsChild();

            _controller.SetRequestState();
            orgView.Controllor.UpdateModel();
            _controller.SetDoneState();
            
        }
    }
}
